#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 1e4 + 5;
const int M = 1e7 + 5;
int n, t;
int w[N], v[N];
ll dp[M];
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> t >> n;
  rep(i, 1, n) cin >> w[i] >> v[i];
  rep(i, 1, n) { rep(j, w[i], t) dp[j] = max(dp[j], dp[j - w[i]] + v[i]); }
  cout << dp[t];
  return 0;
}